﻿using System;
using System.Collections.Generic;
using StringBuilder =  System.Text.StringBuilder;

namespace BotonJXC.Util
{
    public class StringList
    {
        List<string> strings = new List<string>();
        public StringList()
            : this(Environment.NewLine)
        { }

        public StringList(string separator)
            : this(separator, "{0}")
        { }
        public StringList(string separator, string itemFormat)
        {
            this.Separator = separator;
            this.ItemFormat = itemFormat;
        }
        /// <summary>
        /// 獲取或設置分隔符
        /// </summary>
        public string Separator { get; set; }
        /// <summary>
        /// 獲取或設置字符串項顯示格式，默認為　{0}
        /// </summary>
        public string ItemFormat { get; set; }
        public int Count
        {
            get
            {
                return strings.Count;
            }
        }
        public void Add(string item)
        {
            strings.Add(item);
        }
        public void Add(string item, bool split)
        {
            if (split)
            {
                string[] items = item.Split(new string[] { this.Separator }, StringSplitOptions.None);
                Add(items);
            }
            else
                strings.Add(item);
        }
        public void Add(params string[] items)
        {
            foreach (string item in items)
                strings.Add(item);
        }
        public void Clear()
        {
            strings.Clear();
        }
        public void Sort()
        {
            strings.Sort();
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (string s in strings)
            {
                if (sb.Length > 0)
                    sb.Append(this.Separator);
                sb.AppendFormat(this.ItemFormat, s);
            }
            return sb.ToString();
        }
    }
}